/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bussiness;

import Database.CommentData;
import Database.PostData;
import Database.UserData;
import Entity.Comment;
import Entity.Post;
import Entity.User;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author cuong_000
 */
@WebServlet(name = "PostComment", urlPatterns = {"/PostComment"})
public class PostComment extends HttpServlet {

    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String comment = request.getParameter("comment");
        int postid = Integer.parseInt(request.getParameter("postId"));
        response.setContentType("text/html;charset=UTF-8");
        if (!comment.isEmpty() && comment.length() < 500 && comment.length() > 0 && postid != 0) {
            User user = (User) request.getSession().getAttribute("user");
            Comment cmt = setComment(comment, user.getId(), postid);
            PrintWriter out = response.getWriter();
            try {
                boolean check = CommentData.insertComment(cmt);
                if (check) {
                    out.println("<div class=\"media innerAll bg-gray margin-none\">");
                    out.println("<a class=\"pull-left\" href=\"#\"><img src=\"./assets/images/avatar.png\" alt=\"photo\" class=\"media-object\" width=\"35\"></a>");
                    out.println("<div class=\"media-body\")>");
                    out.println("<a class=\"strong text-inverse\">"+user.getLname() + " " + user.getFname()+"</a> " + comment + "");
                    out.println("<div class=\"timeline-bottom\">");
                    out.println("<i class=\"fa fa-clock-o\"></i>");
                    out.println("</div>");
                    out.println("</div>");
                    out.println("</div>");
                }
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(PostArt.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(PostArt.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                out.close();
            }
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private Comment setComment(String comment, int userid, int postid) {
        Comment cmt = new Comment();
        cmt.setContent(comment);
        cmt.setPost_id(postid);
        cmt.setUsers_id(userid);
        return cmt;
    }

}
